package cn.app.edu.projectpracticeofpracticaltraining.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;

import cn.app.edu.projectpracticeofpracticaltraining.R;
import cn.app.edu.projectpracticeofpracticaltraining.entity.SystemEntity;


public class SystemFragmentAdapter extends RecyclerView.Adapter<SystemFragmentAdapter.myViewHolder> {
    private Context mContext;
    private List<SystemEntity.DataBean> mList;
    private OnClickListener mOnClickListener;

    public SystemFragmentAdapter(Context mContext, List<SystemEntity.DataBean> mList,OnClickListener onClickListener) {
        this.mContext = mContext;
        this.mList = mList;
        this.mOnClickListener=onClickListener;
    }

    @Override
    public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_system_frament,parent,false);


        return new myViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(final myViewHolder holder, int position) {

        final SystemEntity.DataBean dataBean = mList.get(position);

        holder.mTitle.setText(dataBean.getName());

        StringBuilder stringBuilder = new StringBuilder();
        for (SystemEntity.DataBean.ChildrenBean child : dataBean.getChildren()) {
            stringBuilder.append(child.getName()).append("      ");
        }

        holder.mLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnClickListener.setOnClickListener(v,holder.getAdapterPosition(),dataBean);
            }
        });

        holder.mContext.setText(stringBuilder);

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    static class myViewHolder extends RecyclerView.ViewHolder{

        TextView mTitle;
        TextView mContext;
        LinearLayout mLinearLayout;

        public myViewHolder(View itemView) {
            super(itemView);

            mTitle = itemView.findViewById(R.id.tv_title_recycler_item_system_fragment);
            mContext = itemView.findViewById(R.id.tv_context_recycler_item_system_fragment);
            mLinearLayout = itemView.findViewById(R.id.ly_item_recycler_system_fragment);

        }
    }

    public interface OnClickListener{
        void setOnClickListener(View view,int position,SystemEntity.DataBean dataBean);
    }
}
